/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.reports;

//Java
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

/**
 * This class can be used as base class for XMLReaders that generate SAX 
 * events from Java objects.
 */

public abstract class AbstractObjectReader implements XMLReader {
	
    private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
    private static final String NS_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
    
    private Map<String, Boolean> features = new HashMap<String, Boolean>();
    private ContentHandler orgHandler;
    
    /** Proxy for easy SAX event generation */
    protected EasyGenerationContentHandlerProxy handler;
    /** Error handler */
    protected ErrorHandler errorHandler;
	
//********************************************************************************
	
    /**
     * Constructor for the AbstractObjectReader object
     */
    public AbstractObjectReader() {
        setFeature(NAMESPACES, false);
        setFeature(NS_PREFIXES, false);
    }
    
//********************************************************************************
	
	/**
     * @see org.xml.sax.XMLReader#getContentHandler()
     */
    public ContentHandler getContentHandler() {
        return this.orgHandler;
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#setContentHandler(ContentHandler)
     */
    public void setContentHandler(ContentHandler handler) {
        this.orgHandler = handler;
        this.handler = new EasyGenerationContentHandlerProxy(handler);
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#getErrorHandler()
     */
    public ErrorHandler getErrorHandler() {
        return this.errorHandler;
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#setErrorHandler(ErrorHandler)
     */
    public void setErrorHandler(ErrorHandler handler) {
        this.errorHandler = handler;
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#getDTDHandler()
     */
    public DTDHandler getDTDHandler() {
        return null;
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#setDTDHandler(DTDHandler)
     */
    public void setDTDHandler(DTDHandler handler) {
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#getEntityResolver()
     */
    public EntityResolver getEntityResolver() {
        return null;
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#setEntityResolver(EntityResolver)
     */
    public void setEntityResolver(EntityResolver resolver) {
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#getProperty(String)
     */
    public Object getProperty(java.lang.String name) {
        return null;
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#setProperty(String, Object)
     */
    public void setProperty(java.lang.String name, java.lang.Object value) {
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#getFeature(String)
     */
    public boolean getFeature(java.lang.String name) {
        return ((Boolean) features.get(name)).booleanValue();
    }
	
//********************************************************************************
	
    /**
     * Returns true if the NAMESPACES feature is enabled.
     * @return boolean true if enabled
     */
    protected boolean isNamespaces() {
        return getFeature(NAMESPACES);
    }
	
//********************************************************************************
	
    /**
     * Returns true if the MS_PREFIXES feature is enabled.
     * @return boolean true if enabled
     */
    protected boolean isNamespacePrefixes() {
        return getFeature(NS_PREFIXES);
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#setFeature(String, boolean)
     */
    public void setFeature(String name, boolean value) {
        this.features.put(name, new Boolean(value));
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#parse(String)
     */
    public void parse(String systemId) throws IOException, SAXException {
        throw new SAXException(
            this.getClass().getName() + " cannot be used with system identifiers (URIs)");
    }
	
//********************************************************************************
	
    /**
     * @see org.xml.sax.XMLReader#parse(InputSource)
     */
    public abstract void parse(InputSource input) throws IOException, SAXException;
		
//********************************************************************************
}

